package dao;

import dao.impl.ComputerDaoImpl;
import entity.ComputerRoom;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ComputerDao extends BaseDao implements ComputerDaoImpl
{
    ComputerRoom computerRoom = null;
    List<ComputerRoom> list = new ArrayList<>();
    @Override
    public List<ComputerRoom> show() {
        super.init();
        conn = super.getConnection();
        try {
            computerRoom = new ComputerRoom();
            pstmt = conn.prepareStatement("select * from computerroom");
            rs = pstmt.executeQuery();
            while (rs.next()){
computerRoom = new ComputerRoom();
                computerRoom.setId(rs.getString("roomId"));
                computerRoom.setLocation(rs.getString("location"));
                computerRoom.setManager(rs.getString("manager"));
                computerRoom.setName(rs.getString("roomName"));
                computerRoom.setNumber(rs.getString("computerNumber"));
                computerRoom.setState(rs.getString("state"));
                list.add(computerRoom);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int delete(int id) {
        int ok = 0;
        super.init();
        conn = super.getConnection();
        try {
            pstmt = conn.prepareStatement("delete from computerroom where roomId=?");
            pstmt.setInt(1,id);
            ok = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ok;
    }

    @Override
    public int add(ComputerRoom computerRoom) {
        int add = 0;
        super.init();
        conn = super.getConnection();
        try {
            pstmt = conn.prepareStatement("INSERT INTO `microblog`.`computerroom`(`roomName`,`computerNumber`,`manager`,`location`,`state`)VALUES(?,?,?,?,?)");
            pstmt.setString(1,computerRoom.getName());
            pstmt.setString(2,computerRoom.getNumber());
            pstmt.setString(3,computerRoom.getManager());
            pstmt.setString(4,computerRoom.getLocation());
            pstmt.setString(5,computerRoom.getState());
            add = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return add;
    }
}
